package com.lin.utils;

import org.apache.commons.lang.math.RandomUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.junit.Test;

import javax.swing.plaf.synth.SynthSpinnerUI;

/**
 * MD5加密工具
 */
public class MD5Util {

    //	盐值(固定部分，增加安全性)
    public static final String SALT = "panda";
    //	加密算法
    private static final String ALGORITH_NAME = "MD5";
    //	加密次数
    private static final int HASH_ITERATIONS = 1024;

    public static String encrypted(String username, String password) {
        return new SimpleHash(ALGORITH_NAME, password,
                ByteSource.Util.bytes(username + SALT),
                HASH_ITERATIONS).toHex();
    }

    @Test
    public void password() {
        String username = "lisi";
        String password = "1";
        for (int i = 0; i < 20; i++) {
            System.out.print(RandomUtils.nextInt(10) + "" + RandomUtils.nextInt(10) + ",");
        }
        String newPassword = new SimpleHash(ALGORITH_NAME, password, ByteSource.Util.bytes(username + SALT), HASH_ITERATIONS).toHex();
        System.out.println("配置文件:" + newPassword);
        String admin = encrypted(username, password);
        System.out.println("该类自定义:" + admin);
    }

}

